﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace DataServer
{
    class AppCommon
    {
        public static string GetValue(object data, string key)
        {
            string returnval = "";
            MemoryStream stream = null;
            Dictionary<string, string> Dic = null;

            try
            {
                if (data != null)
                {
                    if (data.GetType().Name != "Dictionary`2")
                    {
                        IFormatter formatter = new BinaryFormatter();
                        stream = new MemoryStream((byte[])data);
                        Dic = (Dictionary<string, string>)formatter.Deserialize(stream);
                    }
                    else
                    {
                        Dic = (Dictionary<string, string>)data;
                    }

                    if (Dic.ContainsKey(key))
                    {
                        returnval = Dic[key];
                    }
                }
            }
            catch
            {
                stream.Close();
            }
            return returnval;
        }
    }
}
